package git.bankview.wl.common;

import java.util.*;
import java.text.*;
import java.sql.*;

import laya.semi.process.*;

import git.bankview.sl.db.*;

import org.apache.log4j.Logger;

public class InsertTableActivity
	implements IGenericClassActivity
{
	private static Logger log = Logger.getLogger(InsertTableActivity.class);

	public void execute(IProcessContext oCtx)
		throws Exception
	{
		Connection oConn = DBConnectionFactory.getCoreConnection();

		try
		{
			String oTableName = oCtx.getActivityParameter();
			StringBuffer oSql = new StringBuffer("SELECT * FROM ").append(oTableName).append(" WHERE 1 <> 1");
			Statement st = oConn.createStatement();
			ResultSet rs = st.executeQuery(oSql.toString());
			ResultSetMetaData meta = rs.getMetaData();
			oSql = new StringBuffer("INSERT INTO ").append(oTableName).append(" VALUES(");
			for (int i = 0; i < meta.getColumnCount(); ++i)
			{
				if (0 < i)
					oSql.append(", ?");
				else
					oSql.append("?");
			}
			oSql.append(")");
			log.debug(oSql);
			PreparedStatement ps = oConn.prepareStatement(oSql.toString());
			for (int i = 0; i < meta.getColumnCount(); ++i)
			{
				ps.setObject(i + 1, oCtx.getField(meta.getColumnName(i + 1).toLowerCase()));
			}
			ps.executeUpdate();
		}
		finally
		{
			oConn.close();
		}
	}
}
